import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/ad_banner/ad_banner.dart';
import 'package:pornhub_app/modules/sys_partner_app_list_by_station/view.dart';
import 'package:pornhub_app/sys_station_model/sys_station_model.dart';
import 'package:pornhub_app/utils/enum.dart';
import 'package:pornhub_app/utils/extension.dart';

import 'home_controller.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final controller = Get.find<HomePageController>();

  Widget _buildAd() {
    return const SizedBox(
      width: double.infinity,
      // height: 166.w,
      // decoration: BoxDecoration(
      //   borderRadius: BorderRadius.circular(12.w),
      //   color: Colors.amber,
      // ),
      child: AdBanner(adress: AdPlaceNameEnum.NDD_BANNER),
      // child: const Column(
      //   children: [],
      // ),
    );
  }

  Widget _buildTabBar() {
    return Obx(() => controller.stationList.isEmpty
        ? const SizedBox.shrink()
        : TabBar(
            isScrollable: true,
            dividerColor: Colors.transparent,
            tabAlignment: TabAlignment.start,
            indicator: UnderlineTabIndicator(
              borderRadius: BorderRadius.circular(1.5),
              borderSide: const BorderSide(
                color: Color(0xffff9000),
                width: 3,
              ),
            ),
            indicatorPadding: EdgeInsets.only(
              left: 16.w,
              right: 16.w,
            ),
            labelStyle: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
              color: const Color(0xffffffff),
            ),
            unselectedLabelStyle: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
              color: const Color(0xffffffff).withValues(alpha: 0.6),
            ),
            tabs: controller.stationList
                .map((e) => Tab(
                      text: e.name ?? '',
                      height: 22.h,
                    ))
                .toList(),
          ));
  }

  Widget _buildSiteDomain() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        Text(
          "本站永久域名:",
          style: TextStyle(
              color: Colors.white,
              fontSize: 14.sp,
              fontWeight: FontWeight.w500),
        ),
        Obx(() => Text(
              controller.siteDomain.value,
              style: TextStyle(
                  color: const Color(0xffff9307),
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w500),
            )),
      ],
    );
  }

  Widget _buildSingleChildScrollView(SysStationModel station) {
    return SingleChildScrollView(
      child: Column(
        children: [
          if (controller.siteDomain.isNotEmpty) _buildSiteDomain(),
          SysPartnerAppListByStationView(
            styleType: 1,
            stationId: station.stationId ?? 0,
          ),
        ],
      ),
    );
  }

  Widget _buildTabBarView() {
    return Obx(() => TabBarView(
        children: controller.stationList
            .map((e) => _buildSingleChildScrollView(e).keepAlive)
            .toList()));
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Padding(
      padding: EdgeInsets.symmetric(horizontal: 14.0.w),
      child: Obx(
        () => DefaultTabController(
          length: controller.stationList.length,
          child: Column(
            spacing: 14.w,
            children: [
              _buildAd(),
              _buildTabBar(),
              Expanded(
                child: _buildTabBarView(),
              ),
            ],
          ),
        ),
      ),
    ));
  }
}
